<template>
  <CompOperate
      :field="field"
      :designer="designer"
      :design-state="designState"
      :parent-list="parentList"
      :parent-widget="parentWidget"
      :sub-form-row-id="subFormRowId"
      :sub-form-row-index="subFormRowIndex"
      :sub-form-col-index="subFormColIndex"
      :index-of-parent-list="indexOfParentList"
      :display-style="field['options'].displayStyle"
  >
    <div class="scene-public-room-doctor" :style="getStyle('area')" data-sing="信息发布医生信息">
      <!--科室信息-->
      <template v-for="(item,index) in getRoomDoctorInfo">
        <div class="dept-room-default" :class="getClassName" :key="index" :style="getStyle('roomInfo')">
          <p class="dept" v-if="deptRoomshow('deptName')" :style="getStyle('deptName')">
            <ScrollX>{{ item.terminalDept || '\u00a0' }}</ScrollX>
          </p>
          <p class="room" v-if="deptRoomshow('roomName')" :style="getStyle('roomName')">
            <ScrollX>
              {{ item.treatmentName || '\u00a0' }}
            </ScrollX>
          </p>
        </div>
        <div class="doctor-info">
          <div class="doctor-brief">
            <div class="left-wrap">
              <img :src="getDoctorPicture(item)" alt="医生" @error='handleError'/>
            </div>
            <div class="right-wrap">
              <div class="name item" v-if="doctorInfoShow('workerName')" :style="getStyle('workerName')">
                {{ (item.workerName) || '\u00a0' }}
              </div>
              <div class="level item" v-if="doctorInfoShow('workerTitle')" :style="getStyle('workerTitle')">
                <ScrollX>{{ (item.workerTitle) || '\u00a0' }}</ScrollX>
              </div>
              <div class="educational-level item" v-if="doctorInfoShow('workerDegree')"
                   :style="getStyle('workerDegree')">
                <ScrollX>
                  {{ (item.workerDegree) || '\u00a0' }}
                </ScrollX>
              </div>
              <div class="industry-level item" v-if="doctorInfoShow('deptName')" :style="getStyle('doctorDeptName')">
                <ScrollX>
                  {{ (item.doctorDept) || '\u00a0' }}
                </ScrollX>
              </div>
            </div>
          </div>
          <div class="doctor-introduce">
            <div class="doctor-sign" :style="getStyle('doctorIntroduceSign')">医生介绍</div>
            <div class="doctor-content" :style="getStyle('doctorIntroduceContent')">
              <ScrollY :content="(item.workerIntro) || '\u00a0'" :pageDuration="10"/>
            </div>
          </div>
        </div>
      </template>
    </div>
  </CompOperate>
</template>

<script>
import {v4 as uuid} from "uuid";
import {pxToVw} from "@/utils/common";
import emitter from "@/utils/designer/emitter";
import ScrollX from "@c/public/seamlessScroll/ScrollX.vue";
import ScrollY from "@c/public/seamlessScroll/ScrollY.vue";
import mixin from "@sc/templateDetails/modules/homeMiddle/fieldMixin";
import CompOperate from "@c/public/form-designer/devType/modules/compOperate.vue";

export default {
  name: "scene-public-room-doctor-widget",

  componentName: "FieldWidget", //必须固定为FieldWidget，用于接收父级组件的broadcast事件

  mixins: [emitter, mixin],

  components: {
    ScrollY,
    ScrollX,
    CompOperate,
  },

  props: {
    widget: Object,
    field: Object,
    parentWidget: Object,
    parentList: Array,
    indexOfParentList: Number,
    designer: Object,
    designState: {
      type: Boolean,
      default: false,
    },
    devType: {
      type: Boolean,
      default: false,
    },
    subFormRowIndex: {
      /* 子表单组件行索引，从0开始计数 */ type: Number,
      default: -1,
    },
    subFormColIndex: {
      /* 子表单组件列索引，从0开始计数 */
      type: Number,
      default: -1,
    },
    subFormRowId: {
      /* 子表单组件行Id，唯一id且不可变 */
      type: String,
      default: "",
    },
    headTitle: {
      type: String,
      default: "",
    },
    visibleTitle: {
      type: Boolean,
    },
  },

  data() {
    return {
      optionsInfo: Array(),
      widgetKey: uuid(),
      header: require("@a/images/design/header.jpeg"),
      header2: require("@a/images/design/header.png"),
      pause: require("@a/images/design/pause.png"),
    }
  },

  computed: {
    // 设置的行内样式
    getStyle() {
      return function (val) {
        const self = this;
        let info = (self.optionsInfo) || Array();
        let Obj = Object();
        switch (val) {
          case 'head':
            Reflect.set(Obj, 'height', pxToVw(info.headHeight));
            Reflect.set(Obj, 'backgroundColor', `${(info?.backgroundColor)}`);
            return Obj;
          case 'picture':
            Reflect.set(Obj, 'height', pxToVw(info.imageHeight));
            Reflect.set(Obj, 'width', pxToVw(info.imageWidth));
            Reflect.set(Obj, 'borderRadius', pxToVw(info.imageRadius));
            return Obj;
          case 'deptName':
            Reflect.set(Obj, 'fontSize', pxToVw(info.deptNameFontSize));
            return Obj;
          case 'roomName':
            Reflect.set(Obj, 'fontSize', pxToVw(info.roomNameFontSize));
            return Obj;
          case 'workerName':
            Reflect.set(Obj, 'fontSize', pxToVw(info.workerNameFontSize));
            return Obj;
          case 'workerTitle':
            Reflect.set(Obj, 'fontSize', pxToVw(info.workerTitleFontSize));
            return Obj;
          case 'workerDegree':
            Reflect.set(Obj, 'fontSize', pxToVw(info.workerDegreeFontSize));
            return Obj;
          case 'doctorDeptName':
            Reflect.set(Obj, 'fontSize', pxToVw(info.doctorDeptNameFontSize));
            return Obj;
          case 'doctorIntroduceSign':
            Reflect.set(Obj, 'fontSize', pxToVw(info.doctorIntroduceSignFontSize));
            Reflect.set(Obj, 'color', (info?.doctorIntroduceSignBackground));
            return Obj;
          case 'doctorIntroduceContent':
            Reflect.set(Obj, 'fontSize', pxToVw(info.doctorIntroduceContentFontSize));
            return Obj;
          case 'roomInfo':
            Reflect.set(Obj, 'background', (info?.deptRoomInfoBackground));
            Reflect.set(Obj, 'color', (info?.deptRoomInfoFontColor));
            return Obj;
        }
      }
    },

    //顶部科室和诊室的claasName
    getClassName() {
      let {defaultShowCol = ['deptName', 'roomName']} = this.optionsInfo;
      let length = defaultShowCol?.length;
      switch (length) {
        case 1:
          return 'dept-room-1'
        case 2:
          return 'dept-room-default'
      }
    },

    //顶部科室诊室显示控制
    deptRoomshow() {
      return function (row) {
        let {defaultShowCol = ['deptName', 'roomName']} = this.optionsInfo;
        return defaultShowCol.includes(row)
      }
    },

    doctorInfoShow() {
      return function (row) {
        let {doctorInfoDefaultShowCol = ['workerName', 'workerTitle', 'workerDegree', 'deptName']} = this.optionsInfo;
        return doctorInfoDefaultShowCol.includes(row)
      }
    },

    // 获取全量医患信息
    getRoomDoctorInfo() {
      let devType = (this.devType);
      let roomDoctorInfo = this.$store.getters['getPublishRoomDoctorInfo'];
      let roomInfo = devType? (roomDoctorInfo.apiData) : (roomDoctorInfo.mockData) || Array();
      if (!!(roomInfo?.length)) {
        return roomInfo.slice(0, 1);
      } else {
        return Array(1).fill({});
      }
    },

    // 获取医生头像
    getDoctorPicture() {
      return function (row) {
        let isPause = !!(row?.isPause);
        if (!isPause) {
          return (row.workerPicture) || (this.header2)
        } else {
          return this.pause;
        }
      }
    },
  },

  mounted() {
    this.handleOnMounted();
  },

  methods: {
    // 接收父组件传递过来的数据
    async executeMethod(key, data) {
      switch (key) {
        case "publishListInfo":
          this.widgetKey = uuid();
          let dataInfo = Array();
          if (this.$lodash.isObject(data)) {
            dataInfo.push(data)
          } else if (this.$lodash.isString(data)) {
            let obj = JSON.parse(data);
            dataInfo.push(obj)
          } else {
            dataInfo = this.$lodash.cloneDeep(data)
          }
          await this.$store.dispatch('setPublishRoomDoctorInfo', dataInfo)
          break
      }
    },

    handleError(event) {
      event.target.src = this.header2;
    },
  },

  watch: {
    // 获取配置项
    field: {
      handler: function (val) {
        let key = (val.options.config);
        this.widgetKey = uuid();
        this.optionsInfo = (val.options[key]);
      },
      deep: true,
      immediate: true
    }
  },

  destroyed() {
    this.unregisterFromRefList();
  },
};
</script>

<style lang="scss" scoped>
.scene-public-room-doctor {
  display: flex;
  flex-direction: column;
  padding: 28px 30px;
  color: #333;
  height: 1670px;
  font-size: 40px;
  overflow: hidden;
  background: transparent;
  box-sizing: border-box;

  //科室&诊室
  .dept-room-default {
    padding: 10px 0 10px 0;
    background: #0059B3;
    width: 100%;
    overflow: hidden;
    flex-shrink: 0;
    box-sizing: border-box;
    height: 300px;
    border-radius: 20px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    align-self: center;

    p {
      font-size: 80px;
      font-weight: 500;
      text-align: center;
      padding: 0 24px;
      @include singleEllipsis(100%);
    }

    .dept {
      font-size: 82px;
      line-height: 1.35;
      @include singleEllipsis(100%);
    }

    .room {
      font-size: 112px;
      line-height: 1.35;
      @include singleEllipsis(100%);
    }
  }

  //科室||诊室
  .dept-room-1 {
    display: flex;
    align-items: center;
    align-self: center;

    .dept,
    .room {
      font-size: 134px;
      line-height: 1.35;
    }
  }

  .doctor-info {
    display: flex;
    flex-direction: column;
    height: calc(100% - 300px - 44px);
    flex-grow: 1;
    margin-top: 44px;
    box-sizing: border-box;

    .doctor-brief {
      display: flex;
      flex-direction: row;
      height: 484px;
      overflow: hidden;

      .left-wrap {
        width: 358px;
        height: 100%;
        flex-shrink: 0;
        border-radius: 18px;
        overflow: hidden;
        border: 1px rgba(128, 134, 149, 0.2) solid;

        img {
          height: 100%;
          width: 100%;
          object-fit: cover;
        }
      }

      .right-wrap {
        flex-shrink: 0;
        box-sizing: border-box;
        padding: 0 0 0 30px;
        width: calc(100% - 358px);

        .item {
          width: 100%;
          white-space: nowrap;
          font-weight: 500;
          font-size: 48px;
          line-height: 1.8;
          color: #333333;
          text-align: left;

          :deep(.st-inner) {
            margin: inherit;
          }
        }

        .name {
          font-weight: 500;
          font-size: 80px;
          color: #333333;
          line-height: 112px;
          text-align: left;
        }
      }
    }

    .doctor-introduce {
      margin-top: 34px;
      box-sizing: border-box;
      overflow: hidden;
      border-radius: 20px 20px;
      height: calc(100% - 484px - 34px);

      .doctor-sign {
        width: 100%;
        height: 120px;
        color: #0059B3;
        font-size: 50px;
        font-weight: bolder;
        display: flex;
        align-self: center;
        justify-content: center;
        align-items: center;
        text-align: center;
        overflow: hidden;
        border-bottom: 2px solid #DDDDDD;
        background: linear-gradient(180deg, #FFFFFF 0%, #F8F8F8 100%);
      }

      .doctor-content {
        width: 100%;
        height: calc(100% - 120px);
        background: white;
        overflow: hidden;
        font-size: 40px;
        color: #333333;
        padding: 38px 50px;
        line-height: 1.66;
        box-sizing: border-box;
      }
    }
  }
}
</style>
